package com.aim.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * @AUTO
 * @FILE MvcConfig.java
 * @DATE 2021年4月25日 上午9:44:01
 * @Author AIM
 * @Version
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.aim.controller")
// 表示通过AOP框架暴露该代理对象,AopContext能够访问
@EnableAspectJAutoProxy(exposeProxy = true)
public class SpringWebConfig extends WebMvcConfigurerAdapter {

	// 设置默认编码为UTF-8
	private Charset default_charset = Charset.forName("UTF-8");

	/**
	 * @Description 添加拦截器<br>
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
	}

	/**
	 * @Description 资源访问处理器<br>
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("/static/");
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
	}

	/**
	 * 配置请求视图映射
	 */
	@Bean
	public InternalResourceViewResolver resourceViewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
		resolver.setPrefix("/views/");
		resolver.setSuffix(".jsp");
		resolver.setExposeContextBeansAsAttributes(true);
		return resolver;
	}

	/**
	 * @Description 配置视图解析器（前后缀）<br>
	 */
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		super.configureViewResolvers(registry);
		registry.viewResolver(resourceViewResolver());
	}

	// 设置MessageConverter
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		// 添加一个String处理起
		converters.add(stringHttpMessageConverter());
	}

	@Bean
	public StringHttpMessageConverter stringHttpMessageConverter() {
		StringHttpMessageConverter converter = new StringHttpMessageConverter(default_charset);
		List<MediaType> list = buildDefaultMediaTypes();
		converter.setSupportedMediaTypes(list);
		return converter;
	}

	// 设置响应头信息
	private static List<MediaType> buildDefaultMediaTypes() {
		List<MediaType> list = new ArrayList<MediaType>();
		list.add(MediaType.TEXT_HTML); // 这个必须设置在第一位
		list.add(MediaType.APPLICATION_JSON_UTF8);
		return list;
	}
}
